<?php


namespace App\Http\Controllers\Home;


use App\Model\Basic\Category;
use App\Model\Basic\Content;
use App\Model\Basic\Documents;
use App\Model\Basic\Entity;
use App\Repository\Basic\CategoryRepository;
use App\Repository\Basic\ContentRepository;
use Illuminate\Http\Request;

class HomeController extends BaseController
{
    /**
     * 各个文档模型编号
     * @var array
     */
    protected $entityId = [
        'cases' => 1, // 项目案例
        'news' => 2, // 新闻资讯
        'history' => 3, // 发展历程
        'customers' => 4, // 服务客户

    ];
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        //  项目案例 限制 16条
        $caseLists = ContentRepository::documentList($this->entityId['cases'], -16, [], false);

        //  新闻资讯 限制 10条
        $newLists = ContentRepository::documentList($this->entityId['news'], -10, [], false);

        //  发展历程 不限制条数
        $historyLists = ContentRepository::documentList($this->entityId['history'], 0, [], false);

        //  服务客户 限制15条
        $customerLists = ContentRepository::documentList($this->entityId['customers'], -15, [], false);

        return $this->fetch('index', [
            'caseLists' => $caseLists,
            'newLists' => $newLists,
            'historyLists' => $historyLists,
            'customerLists' => $customerLists,
        ]);
    }

    /**
     * 关于我们
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function about(Request $request)
    {
        return $this->fetch('about', []);
    }

    /**
     * 常见问题
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function wenti(Request $request)
    {
        return $this->fetch('wenti', []);
    }

    /**
     * 联系我们
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function contact(Request $request)
    {
        return $this->fetch('contact', []);
    }

    /**
     * 项目案例
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function cases(Request $request)
    {
        $cateId = $request->get('cate_id', 0);
        $condition = [];
        if ($cateId) {
            $condition['category_id'] = $cateId;
        }

        $lists = ContentRepository::documentList($this->entityId['cases'], $this->pageSize, $condition, false);

        $categoryLists = Category::query()->where('model_id', 'LIKE', $this->entityId['cases'])->get();

        return $this->fetch('cases', [
            'categoryLists' => $categoryLists,
            'lists' => $lists,
            'categoryId' => $cateId,
            'categoryInfo' => Category::query()->find($cateId),
        ]);
    }

    /**
     * 项目案例页面详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function casesDetail(Request $request)
    {
        $id = $request->route('id');
        $detail = Documents::query()->find($id);

        $entityInfo = Entity::query()->find($detail->entity_id);
        $categoryId = $detail->category_id;
        $categoryInfo = Category::query()->find($detail->category_id);
        $detail->content = (new Content())->setTable($entityInfo->table_name)->find($id);
        $categoryLists = Category::query()->where('model_id', 'LIKE', $entityInfo->id)->get();
        Documents::query()->where('id',$id)->increment('click');
        $detail->click++;
        return $this->fetch('cases_detail', [
            'detail' => $detail,
            'entityInfo' => $entityInfo,
            'categoryInfo' => $categoryInfo,
            'categoryLists' => $categoryLists,
            'categoryId' => $categoryId,
        ]);
    }


    /**
     * 新闻资讯
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function news(Request $request)
    {
        $cateId = $request->get('cate_id', 0);
        $condition = [];
        if ($cateId) {
            $condition['category_id'] = $cateId;
        }

        $lists = ContentRepository::documentList($this->entityId['news'], $this->pageSize, $condition, false);

        $categoryLists = Category::query()->where('model_id', 'LIKE', $this->entityId['news'])->get();

        return $this->fetch('news', [
            'categoryLists' => $categoryLists,
            'lists' => $lists,
            'categoryId' => $cateId,
            'categoryInfo' => Category::query()->find($cateId),
        ]);
    }

    /**
     * 新闻页面详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newsDetail(Request $request)
    {
        $id = $request->route('id');
        $detail = Documents::query()->find($id);

        $entityInfo = Entity::query()->find($detail->entity_id);
        $categoryId = $detail->category_id;
        $categoryInfo = Category::query()->find($detail->category_id);
        $detail->content = (new Content())->setTable($entityInfo->table_name)->find($id);
        $categoryLists = Category::query()->where('model_id', 'LIKE', $entityInfo->id)->get();
        Documents::query()->where('id',$id)->increment('click');
        $detail->click++;
        return $this->fetch('news_detail', [
            'detail' => $detail,
            'entityInfo' => $entityInfo,
            'categoryInfo' => $categoryInfo,
            'categoryLists' => $categoryLists,
            'categoryId' => $categoryId,
        ]);
    }
}